
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.iti.jets.gpts.beans;

import com.iti.jets.gpts.pojo.AttributeType;
import com.iti.jets.gpts.pojo.TranServiceType;
import com.iti.jets.gpts.pojo.TranServiceTypeAttribute;
import com.iti.jets.gpts.service.AttributeTypeService;
import com.iti.jets.gpts.service.ViewTransServiceTypeAtt;
import com.sun.jersey.api.client.UniformInterfaceException;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import org.codehaus.jettison.json.JSONException;
import java.util.HashMap;
import java.util.Map;
import javax.faces.application.FacesMessage;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;
import org.primefaces.context.RequestContext;
import org.primefaces.event.RowEditEvent;
import org.primefaces.event.SelectEvent;

/**
 *
 * @author noha
 */
@ManagedBean(name = "AllTransService")
@ViewScoped
public class AllTransServiceTypeBean implements Serializable {

    DataModel<TranServiceType> transTypesArr;
    TranServiceType edittst;

    DataModel<TranServiceTypeAttribute> TSattributeTypesModel;
    int tranid;
    int attid;
    String TSTName;
    String ConfirmEditName;
    List<TranServiceTypeAttribute> ArrTSTA;
    List<TranServiceType> alltstArr;
    List<AttributeType> attArray;
    String AttributesAdded;
    String attValue;
    int attributeId;

    public int getAttributeId() {
        return attributeId;
    }

    public void setAttributeId(int attributeId) {
        this.attributeId = attributeId;
    }

    public String getAttValue() {
        return attValue;
    }

    public void setAttValue(String attValue) {
        this.attValue = attValue;
    }

    public void getAttributesAdded() {
        System.out.println("da5al gowa alattribute addedddddddd");

        boolean checkflag = true;

        if (attValue.equals("")) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error ", "Value Field EMPTY!"));
        } else {
            AttributeTypeService attservice = new AttributeTypeService();
            try {
                attArray = attservice.getAllAttribute();
            } catch (JSONException ex) {
                ex.printStackTrace();
            }
            boolean attNumber = isInteger(attValue);

            for (int i = 0; i < attArray.size(); i++) {
                AttributeType attObject1 = attArray.get(i);
                if (attObject1.getId() == attributeId) {
                    boolean number = attObject1.getIsNumber();
                    if (number != attNumber) {
                        System.out.println("3'alt fe attributeee");
                        checkflag = false;
                        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "", "Invalid Attribute Value!"));
                    }
                    break;
                }

            }
            ViewTransServiceTypeAtt tstService = new ViewTransServiceTypeAtt();
            String newname = tstService.getAttributeName(attributeId);
            if (checkflag == true) {
                for (int i = 0; i < ArrTSTA.size(); i++) {
                    TranServiceTypeAttribute TSTACurrent = ArrTSTA.get(i);
                    System.out.println("nextttt" + TSTACurrent.getAttributeType().getName());
                    if (TSTACurrent.getAttributeType().getName().equals(newname)) {
                        checkflag = false;
                        error();
                        break;
                    }
                }
            }

            if (checkflag == true) {
                System.out.println("sa7 fe attributeee");
                ViewTransServiceTypeAtt TSTService = new ViewTransServiceTypeAtt();
                System.out.println("attidddd" + attributeId);
                TSTService.addTranServiceTypeObject(getTranid(), attributeId, attValue);
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "", "Attribute Added Successfully"));
                int id = getTranid();
                System.out.println("ya idddd" + getTranid());
                try {
                    ArrTSTA = TSTService.getTranServiceTypeIndex(id);
                    TSattributeTypesModel = new ListDataModel(ArrTSTA);
                } catch (UniformInterfaceException ex) {
                    ex.printStackTrace();
                } catch (JSONException ex) {
                    ex.printStackTrace();
                }

            }
        }

    }

    public void setAttributesAdded(String AttributesAdded) {
        this.AttributesAdded = AttributesAdded;
    }

    public List<AttributeType> getAttArray() {
        return attArray;
    }

    public void setAttArray(List<AttributeType> attArray) {
        this.attArray = attArray;
    }

    public String getConfirmEditName() {
        return ConfirmEditName;
    }

    public void setConfirmEditName(String ConfirmEditName) {
        this.ConfirmEditName = ConfirmEditName;
    }

    public String getTSTName() {
        return TSTName;
    }

    public void setTSTName(String TSTName) {
        this.TSTName = TSTName;
    }

    public int getAttid() {
        return attid;
    }

    public void setAttid(int attid) {
        this.attid = attid;
    }

    public DataModel<TranServiceTypeAttribute> getTSattributeTypesModel() {
        return TSattributeTypesModel;
    }

    public void setTSattributeTypesModel(DataModel<TranServiceTypeAttribute> TSattributeTypesModel) {
        this.TSattributeTypesModel = TSattributeTypesModel;
    }

    public int getTranid() {
        System.out.println("iddddget" + tranid);
        return tranid;
    }

    public void setTranid(int tranid) {
        this.tranid = tranid;
    }

    public TranServiceType getEdittst() {
        return edittst;
    }

    public void setEdittst(TranServiceType edittst) {
        this.edittst = edittst;
    }

    public DataModel<TranServiceType> getTransTypesArr() {
        return transTypesArr;
    }

    public void setTransTypesArr(DataModel<TranServiceType> transTypesArr) {
        this.transTypesArr = transTypesArr;
    }

    public AllTransServiceTypeBean() {
        System.out.println("constttttttt");
        ViewTransServiceTypeAtt tst = new ViewTransServiceTypeAtt();
        try {
            alltstArr = tst.getTranServiceTypes();
            transTypesArr = new ListDataModel(alltstArr);
        } catch (UniformInterfaceException ex) {
            ex.printStackTrace();
        } catch (JSONException ex) {
            ex.printStackTrace();
        }
    }

    public void viewFun() {
        System.out.println("======soso z3laanna heya el 3'altana viw funnnn======================s=========d=======f==========g====");
        ViewTransServiceTypeAtt tst = new ViewTransServiceTypeAtt();
        try {
            transTypesArr = new ListDataModel(tst.getTranServiceTypes());
        } catch (UniformInterfaceException ex) {
            ex.printStackTrace();
        } catch (JSONException ex) {
            ex.printStackTrace();
        }
    }

    public void editName() {
        String newname = getTSTName();
        if (newname.equals("")) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error!", "Please enter Transportation Service Name"));
        } else {
            boolean flag = false;
            ViewTransServiceTypeAtt TSTAService = new ViewTransServiceTypeAtt();
            try {
                ArrayList<TranServiceType> TSTAName = new ArrayList();
                TSTAName = (ArrayList) TSTAService.getTranServiceTypes();
                for (int i = 0; i < TSTAName.size(); i++) {
                    String TSTAname = TSTAName.get(i).getName();
                    if (TSTAname.equals(newname)) {
                        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error!", "This Transportation Service Name Already exist"));
                        flag = true;
                        break;
                    }
                }
            } catch (UniformInterfaceException ex) {
                ex.printStackTrace();
            } catch (JSONException ex) {
                ex.printStackTrace();
            }

            if (flag == false) {
                int id = getTranid();
                ViewTransServiceTypeAtt tst = new ViewTransServiceTypeAtt();
                String y = tst.setTransportationTypeName(id, newname);
                System.out.println("yyyyyyyyyyyyyyyy" + y);
                if (y.equals("editnamedone")) {
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "", "Name Edited Successfully"));
//                setConfirmEditName("Name Edit Successfully");
//                System.out.println("confirm" + ConfirmEditName);
                } else {
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "", "Name Doesnot Edit Successfully"));
                    //setConfirmEditName("Name Doesnot Edit Successfully");
                }
                viewFun();
            }
        }
    }

    public void error() {
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error!", "Sry,this Attribute name already exist"));
    }

    public static boolean isInteger(String s) {
        try {
            Integer.parseInt(s);
            //Integer.parseInt(s);
        } catch (NumberFormatException e) {
            return false;
        }
        // only got here if we didn't return false
        return true;
    }

    public void onEdit(RowEditEvent event) {
        System.out.println("da5al aleditttttttttttttttttttttttttttttttt");

        TranServiceTypeAttribute TSTAEdited = TSattributeTypesModel.getRowData();
        int currentindex = TSattributeTypesModel.getRowIndex();
        System.out.println("atttttttttt name" + TSTAEdited.getAttributeType().getName());
        String newname = TSTAEdited.getAttributeType().getName();
        boolean flag = true;
//        int i=0;
//       while(TSattributeTypesModel.iterator().hasNext())
//       {
//          TranServiceTypeAttribute TSTACurrent =TSattributeTypesModel.iterator().next();
//          if(TSTACurrent.getAttributeType().getName().equals(newname)&&i!=currentindex)
//          {flag=false; error(); break;}
//          i=i++;
//       }   
        if (TSTAEdited.getValue().equals("")) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error ", "Value Field EMPTY!"));
            System.out.println("value b nulllllllllllllll");
            flag = false;
        } else {

            AttributeTypeService attservice = new AttributeTypeService();
            try {
                attArray = attservice.getAllAttribute();
            } catch (JSONException ex) {
                ex.printStackTrace();
            }
            boolean attNumber = isInteger(TSTAEdited.getValue());
            System.out.println("isnumber you enter" + attNumber);
            for (int i = 0; i < attArray.size(); i++) {
                AttributeType attObject1 = attArray.get(i);
                System.out.println("attobjectdetails" + attObject1.getId() + attObject1.getName() + attObject1.getIsNumber());
                if (attObject1.getName().equals(TSTAEdited.getAttributeType().getName())) {
                    boolean number = attObject1.getIsNumber();
                    System.out.println("isnumber in database" + number);
                    if (number != attNumber) {
                        System.out.println("3'alt fe attributeee");
                        flag = false;
                        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "", "Invalid Attribute Value!"));
                    }
                    break;
                }
            }
        }
        if (flag == true) {
            for (int i = 0; i < ArrTSTA.size() && flag == true; i++) {

                TranServiceTypeAttribute TSTACurrent = ArrTSTA.get(i);
                System.out.println("nextttt" + TSTACurrent.getAttributeType().getName());
                if (TSTACurrent.getAttributeType().getName().equals(newname) && i != currentindex) {
                    flag = false;
                    error();
                    break;
                }
            }
        }

        //System.out.println("rowcount" + TSattributeTypesModel.getRowCount() + flag);
        if (flag == true) {
            System.out.println("da5al altrueeeeeeeeeeeeeee");
            // int m=TSTAEdited.getAttributeType().getId();
            //      TSTAEdited.getId().setIdAttributeType(m);
            //    TSTAEdited.getId().setIdTranServiceType(getTranid());
            //System.out.println("toooooooot"+"id al attribute"+TSTAEdited.getId().getIdAttributeType());
       /* FacesMessage msg = new FacesMessage("attribute Edited", ((TranServiceTypeAttribute) event.getObject()).getAttributeType().getName());
             FacesContext.getCurrentInstance().addMessage(null, msg);
             */
            System.out.println("idaltransportation" + getTranid() + "value " + TSTAEdited.getValue() + " name " + TSTAEdited.getAttributeType().getName() + " " + "idalattribute" + TSTAEdited.getAttributeType().getId() + " is Updated Value...............................");
            for (int i = 0; i < ArrTSTA.size(); i++) {
                //  System.out.println("editallarray" + ArrTSTA.get(i).getValue() + ArrTSTA.get(i).getId().getIdAttributeType());
            }

            try {
                JSONArray TSTAJsonArr = new JSONArray();
                JSONObject jsontranservcetype1 = new JSONObject();
                jsontranservcetype1.put("transid", getTranid());
                TSTAJsonArr.put(jsontranservcetype1);

                for (int i = 0; i < ArrTSTA.size(); i++) {
                    JSONObject jsontranservcetype = new JSONObject();
                    JSONObject tsta = new JSONObject();
                    tsta.put("attid", ArrTSTA.get(i).getAttributeType().getId());
                    tsta.put("attvalue", ArrTSTA.get(i).getValue());
                    jsontranservcetype.put("attributes", tsta);
                    TSTAJsonArr.put(jsontranservcetype);
                    ViewTransServiceTypeAtt tstService = new ViewTransServiceTypeAtt();
                    tstService.UpdateTransSerTypeAtt1(TSTAJsonArr);
                }
            } catch (JSONException ex) {
                ex.printStackTrace();
            }

//                ViewTransServiceTypeAtt tstService = new ViewTransServiceTypeAtt();
//                // int attid=tstService.getAttributeID(TSTAEdited.getAttributeType().getName());
//                tstService.UpdateTransSerTypeAtt(TSTAEdited.getAttributeType().getId(), getTranid(), TSTAEdited.getAttributeType().getName(), TSTAEdited.getValue());
        }
    }

    public void onCancel(RowEditEvent event) {
        FacesMessage msg = new FacesMessage("Car Cancelled", ((TranServiceTypeAttribute) event.getObject()).getAttributeType().getName());

        FacesContext.getCurrentInstance().addMessage(null, msg);
        // System.err.println("OnCancel Hereeeeeeeeeeeeeeeeeee");

    }

    public void deleteTS() {
        try {
            TranServiceType TSobject = transTypesArr.getRowData();
            ViewTransServiceTypeAtt tstservice = new ViewTransServiceTypeAtt();
            tstservice.removeTS(TSobject.getId());
            alltstArr = tstservice.getTranServiceTypes();
            transTypesArr = new ListDataModel(alltstArr);
        } catch (org.codehaus.jettison.json.JSONException ex) {
            ex.printStackTrace();
        }
    }

    public void deleteTST() {
        try {
            TranServiceTypeAttribute TSTobject = TSattributeTypesModel.getRowData();
            ViewTransServiceTypeAtt tstservice = new ViewTransServiceTypeAtt();
            //  System.out.println("attidddddddddddddddddddddddddddddddd******"+TSTobject.getAttributeType().getId());
            tstservice.removeTST(TSTobject.getAttributeType().getId(), getTranid());
            ArrTSTA = tstservice.getTranServiceTypeIndex(getTranid());
            TSattributeTypesModel = new ListDataModel(ArrTSTA);
        } catch (org.codehaus.jettison.json.JSONException ex) {
            ex.printStackTrace();
        }
    }

    public void loadtst(SelectEvent event) {
        TranServiceType stst = (TranServiceType) event.getObject();
        System.out.println("khalifa khalifa khalifa khalifa khalifa khalifa khalfia ");
        if (stst != null) {
            TSTName = stst.getName();
            tranid = stst.getId();
            System.out.println("hmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm" + tranid);
            ViewTransServiceTypeAtt tst = new ViewTransServiceTypeAtt();
            try {
                int id = getTranid();
                System.out.println("ya idddd" + getTranid());
                ArrTSTA = tst.getTranServiceTypeIndex(id);
                TSattributeTypesModel = new ListDataModel(ArrTSTA);

                for (int i = 0; i < alltstArr.size(); i++) {
                    if (id == alltstArr.get(i).getId()) {
                        TSTName = alltstArr.get(i).getName();
                        System.out.println("nameeeeeeeeeeeeeeeeeeeeeeeeeeeee" + TSTName);
                        break;
                    }
                }

            } catch (UniformInterfaceException ex) {
                ex.printStackTrace();
            } catch (JSONException ex) {
                ex.printStackTrace();
            }
        }

    }

}
